//  File:         TagCollection.cpp
//  Author:       Scott Nordmann & Lauren Wallace
//  Description:  Non-inline member function definitions.
#include "TagCollection.h"
#include "Tag.h"
#include "TagSpec.h"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>

using namespace std;

TagCollection::TagCollection()
{

}

void TagCollection::shutDown(ofstream& ofsave)
{
	ofsave << tagSpecs.size() << endl;
	if (tagSpecs.size() != 0)
	{
		vector<TagSpec*>::iterator iter;
		for(iter=tagSpecs.begin();iter !=tagSpecs.end(); ++iter)
		{
			ofsave << (*iter)->getName() << endl;
			(*iter)->shutDown(ofsave);
		}
	}
}
void TagCollection::display() const
{
	int ctr;
	cout << "Current Tags:" << endl;
	vector<TagSpec*>::const_iterator iter;
	for(iter=tagSpecs.begin(), ctr=1; iter != tagSpecs.end(); ++iter, ++ctr)
	{
		cout << " [" << ctr <<"]";
		(*iter) -> display();
		if (ctr%3==0)
			cout <<endl;
	}
}

TagSpec* TagCollection::chooseTagSpec()const
{
	int choice;
	cout <<endl << endl;
	cout << "Which note would you like (enter number 1 - " << tagSpecs.size() << ")? ";
	cin >> choice;
	// range checked here- if it's out of bounds put choice in bounds
	if (choice<1)
		choice = 1;
	if (choice > tagSpecs.size())
		choice = tagSpecs.size();
			
	--choice;//decriment choice so that it works with vector
	
	return tagSpecs.empty() ? tagSpecs[choice] : NULL;
}

void TagCollection::displayByTag() const
{
	cout << "View Entries by Tag" << endl << endl;

	TagSpec* spec;
	display();
	spec = chooseTagSpec();

	if (spec != NULL)
		spec->displayEntries();
	else
		cout << "Selected tag has no uses." << endl << endl;
}

Tag* TagCollection::tagEntry()
{
	TagSpec* currentTag;
	Tag* tag = NULL;

	if ( ! ( tagSpecs.empty() ) )
		display();

	char command = ' ';

	while (toupper(command) != 'M')
	{
		cout << "Tagging an Entry" << endl << endl;

		cout << "E: Use Existing Tag" << endl;
		cout << "C: Create New Tag" << endl;
		cout << "M: Return to Menu" << endl << endl;
		
		cout << "Please enter your selection: ";
		cin >> command;

		switch (toupper(command))
		{
		case 'E':
			currentTag = useTagSpec();
			command = 'M';
			break;
		
		case 'C':
			system("cls");
			cout << "Creating Tag" << endl << endl;
			currentTag = createTagSpec();
			command = 'M';
			break;
		
		case 'M':
			currentTag = NULL;
			break;

		default:
			cout << "Invalid selection." << endl << endl;
			break;
		}

		cin.ignore();

		system("cls");
	}

	if (currentTag != NULL)
		tag = currentTag->halfTag();

	return tag;
}

TagSpec* TagCollection::useTagSpec()
{
	system("cls");
	TagSpec* spec;
	
	if ( tagSpecs.empty() )
	{
		spec=NULL;
		cout << "You have no tags." << endl << endl;
		
		system("pause");
	}
	else
	{
		display();
		spec=chooseTagSpec();
	}

	return spec;
}

TagSpec* TagCollection::createTagSpec(const string& nam)
{
	TagSpec* spec;
	
	spec=new TagSpec;
	spec->populate(nam);
	tagSpecs.push_back(spec);
	return spec;
}

TagSpec* TagCollection::createTagSpec()
{
	string temp;
	TagSpec* spec;

	temp = setNewTagName();

	if (temp != "")
	{
		spec=new TagSpec;
		spec->populate(temp);
		tagSpecs.push_back(spec);
	}
	else
	{
		cout << "The name you entered is either already used or not valid."<< endl << endl;
		spec=NULL;

		system("pause");
	}
	return spec;
}
Tag* TagCollection::createHalfTag(TagSpec& spec)
{
	return spec.halfTag();
}
void TagCollection::removeHalfTag(Tag& tag)
{
		vector<TagSpec*>::const_iterator iter;
	for(iter=tagSpecs.begin(); iter != tagSpecs.end(); ++iter)
	{
		if (tag.getSpec()==(*iter))
		{
			(*iter)->removeHalfTag(tag);
		}
	}
}
string TagCollection::setNewTagName()
{
	string newName = "";
	vector<TagSpec*>::iterator iter;

	cin.ignore();

	cout <<"Enter new Tag: ";
	getline(cin,newName);

	//test the name
	
	if  ( ! ( tagSpecs.empty() ) )
	{
		for(iter=tagSpecs.begin(); iter != tagSpecs.end() ; ++iter)
		{
			if ( **iter == newName)
				newName = "";
		}
	}
	return newName;
}